﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SPMS.Commons.Configuration;

namespace SPMS.Log.Configuration.Handler
{
    /// <summary>
    /// 日志配置根节点节点处理器
    /// </summary>
    /// <remarks>用于处理日志配置根节点</remarks>
    /// <history>
    /// [zanezeng]               2009/10/15 14:05    创建
    /// </history>
    public sealed class LeyserLog_NodeHandler : AbstractNodeHandler
    {
        /// <summary>
        /// 处理指定的节点
        /// </summary>
        /// <param name="node">配置节点</param>
        /// <param name="context">处理上下文</param>
        /// <returns>根据配置信息创建的对象</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-11-17 11:39    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/10/15 14:06    创建
        /// </history>
        protected override object ProcessInternal( IConfigurationNode node, ParseContext context )
        {
            //获得默认日志记录器属性
            string defaultLoggerName = this.GetAttributeValue( node, LogConfiguationTags.ATTRIBUTE_DEFAULT_LOGGER, context );

            //判断是否设置默认的日志记录器名称
            if (string.IsNullOrEmpty( defaultLoggerName ))
            {
                //抛出异常
                throw new ConfigurationException( string.Format( Properties.Resources.MSG_CONFIGURATION_ATTRIBUTE_REQUIRED, node.Name, LogConfiguationTags.ATTRIBUTE_DEFAULT_LOGGER ), node );
            }

            //输出日志
            if (LoggingContext.MetaLog.IsTraceEnabled)
            {
                LoggingContext.MetaLog.Trace( "解析日志存储器'{0}'的默认日志记录器，默认日志记录器为'{1}'", context.Repository, defaultLoggerName );
            }

            //记录默认的日志记录器
            context.Repository.DefaultLoggerName = defaultLoggerName;

            //读取默认的日志级别
            string defaultLevelName = this.GetAttributeValue( node, LogConfiguationTags.ATTRIBUTE_DEFAULT_LEVEL, context );

            //判断是否设置了默认的日志级别
            if (!string.IsNullOrEmpty( defaultLevelName ))
            {
                //获得默认的日志级别
                Level defaultLevel = context.Repository.LevelMap.LookupLevelWithDefault( defaultLevelName, Level.All );

                //输出日志
                if (LoggingContext.MetaLog.IsTraceEnabled)
                {
                    LoggingContext.MetaLog.Trace( "解析默认日志级别名称'{0}'为日志级别对象'{1}'，并且设置为日志存储器'{2}'的默认日志级别", defaultLevelName, defaultLevel, context.Repository );
                }

                //设置默认的日志级别
                context.Repository.SetDefaultLevel( defaultLevel );
            }

            //用于保存配置节点处理器
            IConfigurationNodeHandler<ParseContext> handler;

            //循环当前配置节点的所有子节点
            foreach (IConfigurationNode subNode in node.ChildNodes)
            {
                //获得配置节点处理器
                handler = context.GetNodeHandler( subNode );

                //判断是否为Null
                if (null == handler)
                {
                    //抛出异常
                    throw new NotSupportedException( string.Format( Properties.Resources.MSG_TAG_NOTSUPPORTED, subNode.Name ) );
                }

                //处理节点
                handler.Porcess( subNode, context );
            }

            //返回日志存储器
            return context.Repository;
        }
    }
}
